package com.ebring.shareEducation.service.impl;

import com.ebring.shareEducation.bean.Video;
import com.ebring.shareEducation.dao.VideoMapper;
import com.ebring.shareEducation.service.VideoService;

import com.ebring.shareEducation.utils.RspMsg;
import com.ebring.shareEducation.utils.WeChat.IdsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Cola
 * Date  2018-12-17 11:20
 */

@Service
@Transactional
public class VideoServiceImpl implements VideoService {

    @Autowired
    VideoMapper videoMapper;

    private static Logger logger = LoggerFactory.getLogger(VideoServiceImpl.class);

    // 上传视频文件
    @Override
    public RspMsg<Video> insert(Video video) {
        RspMsg rspMsg = new RspMsg();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        video.setIdNo(IdsUtil.genOrderNo());
        video.setModifyTime(simpleDateFormat.format(date));
        video.setOperate("1");
        int i = videoMapper.insertSelective(video);
        if (i > 0) {
            logger.info("上传视频文件成功");
            rspMsg.setOK();
            return rspMsg;
        }

        rspMsg.setError();
        rspMsg.setMsg("上传视频失败");
        return rspMsg;
    }


    // 获取视频列表
    @Override
    public RspMsg<Video> select() {
        RspMsg rspMsg = new RspMsg();
        List<Video> videos = videoMapper.selectAll();
        if (videos.isEmpty() || videos == null) {
            rspMsg.setError();
            rspMsg.setMsg("未找到相关数据");
            return rspMsg;
        }

        rspMsg.setOK();
        rspMsg.setResult(videos);
        return rspMsg;
    }


    // 根据视频ID获取视频信息
    @Override
    public RspMsg<Video> selectById(String videoId) {
        RspMsg rspMsg = new RspMsg();
        Video video = videoMapper.selectByPrimaryKey(videoId);
        if (video == null) {
            rspMsg.setError();
            rspMsg.setMsg("未找到该视频");
            return rspMsg;
        }

        rspMsg.setOK();
        rspMsg.setResult(video);
        return rspMsg;
    }
}
